package com.jtjrenren.android.taxi.driver.core.order.history;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.TextView;

import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.bean.api.ApiOrderList;
import com.jtjrenren.android.taxi.driver.core.adapter.OrderHistoryAdapter;
import com.jtjrenren.android.taxi.driver.core.order.details.OrderDetailsActivity;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.driver.utils.UIHelper;
import com.jtjrenren.android.taxi.driver.R;
import com.library.weiget.AutoLoadRecyclerView;
import com.library.weiget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by Administrator on 2016/5/5.
 */
public class OrderHistoryActivity extends BaseActivity {
    @BindView(R.id.order_history_rcy)
    AutoLoadRecyclerView rcy_list;
    @BindView(R.id.order_history_refresh)
    SwipeRefreshLayout wrf;

    private int currentPage = 0;
    private int page_size = 20;

    private List<ApiOrderList.OrderListEntity> orderList;
    private OrderHistoryAdapter historyAdapter;

    private boolean isLoadData = false;
    private DriverInfo driver;

    @Override
    public int getLayoutID() {
        return R.layout.aty_order_history;
    }

    @Override
    public void init() {
        driver = AppManger.getInstance().driver;

        orderList = new ArrayList<>();

        UIHelper.initSwipeRefreshLayout(wrf);
        UIHelper.initVritalReclyView(this, rcy_list);
        rcy_list.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));


        historyAdapter = new OrderHistoryAdapter(this);
        historyAdapter.setList(orderList);
        rcy_list.setAdapter(historyAdapter);

        currentPage = 0;
        setListeners();

        ((TextView) findViewById(R.id.title_title)).setText("历史订单");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        getListFromWeb(currentPage);
    }

    @Override
    protected void onResume() {
        super.onResume();
        currentPage = 0;
        getListFromWeb(currentPage);
    }

    private void setListeners() {
        rcy_list.setLoadMoreListener(new AutoLoadRecyclerView.LoadMoreListener() {
            @Override
            public void loadMore() {
                getListFromWeb(currentPage);
            }
        });

        wrf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 0;
                getListFromWeb(currentPage);
            }
        });
        historyAdapter.setItemClickListener(new OrderHistoryAdapter.ItemClickListener() {
            @Override
            public void callPhone(String phone) {
                UIHelper.showCallPhoneDialog(OrderHistoryActivity.this, phone);
            }

            @Override
            public void itemClick(int position) {
                ApiOrderList.OrderListEntity orderListEntity = orderList.get(position);

//                LogUtil.log(OrderHistoryActivity.class, "选中的订单信息：" + orderListEntity.toString());
//                Order orderByOrderID = AdapterUtils.changerOrderFromWebToLocalOrder(orderListEntity);
//                orderByOrderID.setCreateDate(orderListEntity.getCreateDate());

                startActivity(OrderDetailsActivity.getDiyIntent(OrderHistoryActivity.this, orderListEntity.getOrderID()));
            }
        });
    }

    private void getListFromWeb(int pageIndex) {
        if (driver == null)
            return;

        showProDialog(null, getString(R.string.shujujiazaizhongqingshaohou));

        if (isLoadData)
            return;

        isLoadData = true;

        ApiWorks.getOrderHistrory(driver.getWorkNo(), page_size, pageIndex + 1, new ApiWorks.ResponseListener<ApiOrderList>() {
            @Override
            public void onResponse(ApiOrderList apiOrderList) {
                disProDialog();
                isLoadData = false;

                if (wrf.isRefreshing())
                    wrf.setRefreshing(false);

                if (apiOrderList != null && apiOrderList.getOrderList().size() > 0) {
                    if (currentPage == 0)
                        historyAdapter.replace(apiOrderList.getOrderList());
                    else
                        historyAdapter.appendList(apiOrderList.getOrderList());
                    currentPage++;
                }
            }
        });
    }

}
